﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Coolite.Ext.Web;
using GUESTSMANAGEMENT.Controller;
using System.Collections.Generic;


namespace GUESTSMANAGEMENT
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userErro"] != null)
            {
                Response.Write("<script language=\"Javascript\">");
                Response.Write("alert(\"Usuário ou Senha Inválidos!\");");
                Response.Write("</script>");
                Session.Abandon();
            }
        }

        protected void Teste(object sender, AjaxEventArgs e)
        {
            List<gmuser> listuser = Controller.User.getGridList();

            string textUserName = txtUsername.Text.ToString();
            string textUserPassword = txtPassword.Text.ToString();

            for (int i = 0; i < listuser.Count; i++)
            {
                gmuser user = Controller.User.getByUser(Convert.ToInt32(listuser[i].CDUSER));

                    if ((textUserName == user.IDUSER) && (textUserPassword == user.IDPASSWORD))
                    {
                        if (user.FGENABLED == 1)
                        {
                            Session["userCd"] = user.CDUSER;
                            Session["userId"] = user.IDUSER;
                            Session["userName"] = user.NMUSER;
                            Session["userPhone"] = user.NMPHONE;
                            Session["userMail"] = user.NMMAIL;
                            Session["userPassword"] = user.IDPASSWORD;
                            Session["userEnabled"] = (int)user.FGENABLED;
                            Session["userType"] = (int)user.FGUSER;

                            Response.Redirect("Default.aspx");
                        }
                        else if (user.FGENABLED == 2)
                        {
                            break;
                        }
                    }
                              
            }
            Session["userErro"] = "erro"; 
            Response.Redirect("Login.aspx?error=1");
        }
    }
}
